<?php
class SearchController extends Controller {

	private function loadModel($id){
		$model= Contact::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

    public function actionIndex(){
		
    }

    public function actionContacts(){
    	if(empty($_GET['q']) && is_string($_GET['q'])) die;
    	$query = $_GET['q'];
    	$cond = new CDbCriteria();
    	$cond->compare('created_by', Yii::app()->user->getId());
    	$cond->compare('full_name', $query, true);
    	$cond->compare('email', $query,true,'OR');
    	$search = Contact::model()->findAll($cond);
    	$data = array();
    	foreach($search as $c) {
    		$data[$c->email] = array(
    			'value' => $c->id,
    			'name'	=> trim($c->full_name) . ' [' . trim($c->email) . ']',
    			'email' => trim($c->email)
    		);
    	}
    	$data = array_values($data);
    	echo json_encode($data);
    }


}